<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/02/01 09:31:59 $)
// $RCSfile: report_sla.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->assign("calendar", true);

$interface->staffHeader($_SWIFT["language"]["tickets"]." &gt; ".$_SWIFT["language"]["reports"], 2);

renderTicketTree();
$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; '.'<a href="index.php?_m=tickets&_a=reports&type=worksummary" title="'.$_SWIFT["language"]["reports"].'">'.$_SWIFT["language"]["reports"].'</a> &raquo; '.'<a href="index.php?_m=tickets&_a=reports&type=sla" title="'.$_SWIFT["language"]["repsla"].'">'.$_SWIFT["language"]["repsla"].'</a>', "", 2);

printInfoBox($infomessage);
printErrorBox($errormessage);

?>
<form name="swiftform" action="index.php" method="POST"><fieldset class="swiftfieldset">
<legend><?=$_SWIFT["language"]["filter"]?></legend>
<table width="100%"  border="0" cellspacing="1" cellpadding="2" class="tborder">
<tr>
<?
if (empty($_POST["datefrom"]) && empty($_POST["dateto"]))
{
	$_prevday = DATENOW-86400;
	$_gdatefrom = generateCalendarTimeline($_prevday);
	$_gdateto = generateCalendarTimeline(DATENOW);
} else {
	$_gdatefrom = $_POST["datefrom"];
	$_gdateto = $_POST["dateto"];
}

?>
<td width="100" class="row1" nowrap><?=$_SWIFT["language"]["fildate"]?></td>
<td class="row2"><input type="text" name="datefrom" id="datefrom" size="12" readonly="1" value="<?=iif(!empty($_gdatefrom), $_gdatefrom)?>" class="swifttext" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datefromtrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('datefrom', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /> <?=$_SWIFT["language"]["filto"]?> <input type="text" name="dateto" id="dateto" size="12" readonly="1" value="<?=iif(!empty($_gdateto), $_gdateto)?>" class="swifttext" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" />&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" id="datetotrigger" style="cursor: pointer;" align="absmiddle" onclick="return showCalendar('dateto', '<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>');" /></td>
<td width="100" class="row2" align="right" nowrap><input type="submit" class="yellowbutton" name="filter" value="<?=$_SWIFT["language"]["filter"]?>"></td>
</tr>
</table>
</fieldset><BR />
<input type="hidden" name="_m" value="tickets"><input type="hidden" name="_a" value="reports"><input type="hidden" name="type" value="sla">
</form>
<?
$_datefrom = getCalendarDateline($_gdatefrom);
$_dateto = getCalendarDateline($_gdateto);
$_dateto = mktime(23,59,59,date("m", $_dateto), date("d", $_dateto), date("Y", $_dateto));

// Get List of Tickets between the given time
$_tickeidlist = $_ticketlist = array();
$dbCore->query("SELECT `ticketid`, `dateline`, `departmentid`, `ticketstatusid`, `priorityid`, `slaplanid`, `subject`, `ticketmaskid` FROM `". TABLE_PREFIX ."tickets` WHERE `dateline` > '". intval($_datefrom) ."' AND `dateline` < '". intval($_dateto) ."';");
while ($dbCore->nextRecord())
{
	$_ticketidlist[] = $dbCore->Record["ticketid"];
	$_ticketlist[$dbCore->Record["ticketid"]] = $dbCore->Record;
	$_ticketlist[$dbCore->Record["ticketid"]]["slascore"] = $_ticketlist[$dbCore->Record["ticketid"]]["slareplysuccesshits"] = $_ticketlist[$dbCore->Record["ticketid"]]["slareplycounts"] = 0;
}

// Build staff worked list
$dbCore->query("SELECT `ticketid`, `staffid` FROM `". TABLE_PREFIX ."ticketposts` WHERE `ticketid` IN (". buildIN($_ticketidlist) .") AND `staffid` != '0';");
while ($dbCore->nextRecord())
{
	$_ticketlist[$dbCore->Record["ticketid"]]["staffworked"] .= $_SWIFT["staffcache"][$dbCore->Record["staffid"]]["fullname"]."<BR />";
}

// Now get the audit log entries..
$_ticketlog = array();
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."auditlogs` WHERE `ticketid` IN (". buildIN($_ticketidlist) .") AND `actiontype` IN ('". ACTION_NEWTICKET ."', '". ACTION_NEWREPLY ."');");
while ($dbCore->nextRecord())
{
	$_ticket = &$_ticketlist[$dbCore->Record["ticketid"]];

	if ($dbCore->Record["actiontype"] == ACTION_NEWTICKET && !empty($_ticket["slaplanid"]) && empty($_ticket["slacustomerreply"]))
	{
		$_ticket["slacustomerreply"] = $dbCore->Record["dateline"];
	} else if ($dbCore->Record["actiontype"] == ACTION_NEWREPLY && empty($dbCore->Record["staffid"]) && !empty($_ticket["slaplanid"]) && empty($_ticket["slacustomerreply"])) {
		$_ticket["slacustomerreply"] = $dbCore->Record["dateline"];
	} else if ($dbCore->Record["actiontype"] == ACTION_NEWREPLY && !empty($dbCore->Record["staffid"]) && !empty($_ticket["slaplanid"]) && !empty($_ticket["slacustomerreply"])) {
		// First get the seconds in which the reply should have been made..
		$_slaResult = getDueTimeline($_ticket["departmentid"], $_ticket["ticketstatusid"], $_ticket["priorityid"], $_ticket["slaplanid"]);
		$_diffseconds = $_slaResult["duediff"];

		// Get the diff between staff and customer reply
		$_staffdiff = $dbCore->Record["dateline"]-$_ticket["slacustomerreply"];
		// Was it handled on time?
		if ($_staffdiff < $_diffseconds)
		{
			$_ticket["slareplysuccesshits"]++;
		} else {
			$_ticket["slareplysuccesshits"]--;
		}

		$_ticket["slareplycounts"]++;
	}
}

?>
<table width="100%"  border="0" cellspacing="1" cellpadding="3" class="tborder">

<tr class="tabletitlerow">
<td width="80">&nbsp;<?=$_SWIFT["language"]["f_ticketid"]?></td>
<td>&nbsp;<?=$_SWIFT["language"]["f_subject"]?></td>
<td width="120" align="center">&nbsp;<?=$_SWIFT["language"]["f_department"]?></td>
<td width="100" align="center">&nbsp;<?=$_SWIFT["language"]["f_ticketstatus"]?></td>
<td width="90" align="center">&nbsp;<?=$_SWIFT["language"]["f_priority"]?></td>
<td width="150" align="center">&nbsp;<?=$_SWIFT["language"]["f_staffworked"]?></td>
<td width="80" align="center">&nbsp;<?=$_SWIFT["language"]["f_sla"]?></td>
</tr>
<?
foreach ($_ticketlist as $key=>$val)
{
	?>
	<tr class="<?=getRowBG();?>">
	<td width="80">&nbsp;<a href="index.php?_m=tickets&_a=viewticket&ticketid=<?=$val["ticketid"]?>"><?=iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"])?></a></td>
	<td>&nbsp;<a href="index.php?_m=tickets&_a=viewticket&ticketid=<?=$val["ticketid"]?>"><?=htmlspecialchars($val["subject"])?></a></td>
	<td width="120" align="center">&nbsp;<?=$_SWIFT["departmentcache"][$val["departmentid"]]["title"]?></td>
	<td width="100" align="center">&nbsp;<?='<font color=\''. $_SWIFT["statuscache"][$val["ticketstatusid"]]["statuscolor"] .'\'>'.htmlspecialchars($_SWIFT["statuscache"][$val["ticketstatusid"]]["title"]).'</font>'?></td>
	<td width="90" align="center">&nbsp;<?='<font color=\''. $_SWIFT["prioritycache"][$val["priorityid"]]["frcolorcode"] .'\'>'.htmlspecialchars($_SWIFT["prioritycache"][$val["priorityid"]]["title"]).'</font>'?></td>
	<td width="150" align="center">&nbsp;<?=$val["staffworked"]?></td>
	<td width="120" align="center">&nbsp;<b><?
	$_success = $val["slareplysuccesshits"];
	if (empty($val["slaplanid"]))
	{
		echo '<font color="#FF8C5A">'.$_SWIFT["language"]["nosla"];
	} else if (empty($val["slareplycounts"])) {
		echo '<font color="#CCCCCC">'.$_SWIFT["language"]["notreplied"];
	} else if ($val["slareplysuccesshits"] <= 0) {
		echo '<font color="#CF5D60">'."0%";
	} else {
		echo '<font color="#8BB467">'.number_format((($_success/$val["slareplycounts"])*100), 2)."%";
	}
	?></font></b></td>
	</tr>
	<?
}
?>
</table>
<?

$template->assign("backurl", "index.php?_m=tickets&_a=manage");

$interface->staffFooter();
?>